package com.lenovo.lmrp.controller;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lenovo.lmrp.api.assigntrace.ITraceWorkflow;
import com.lenovo.lmrp.api.itsmgateway.IItsmProject;
import com.lenovo.lmrp.api.itsmgateway.IItsmTicket;
import com.lenovo.lmrp.api.model.RestResponse;
import com.lenovo.lmrp.api.model.entity.AExtensePO;
import com.lenovo.lmrp.api.model.entity.ProjectPO;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.lmrp.api.model.view.AttachmentVO;
import com.lenovo.lmrp.api.model.view.TicketProcessVO;
import com.lenovo.lmrp.api.model.view.TicketVO;
import com.lenovo.lmrp.util.RestClientFactory;
import com.lenovo.sdc.framework.common.base.BaseRestSpringController;
import com.lenovo.sdc.framework.rest.RestClientProxy;
import com.lenovo.sdc.framework.util.JSONHelper;
import com.lenovo.sdc.framework.util.PageData;


/**
 * 
 * 
 * @author feipeng
 * @version $Id: TicketController.java, v 0.1 2016年5月20日 下午2:40:23 feipeng Exp $
 */
@Controller
public class TicketController extends BaseRestSpringController {
	@Autowired
    RestClientFactory restClientFactory = new RestClientFactory();
	
	@RequestMapping(value="/showticket")
    public String index() throws Exception {
        return "/ticket/show";
    }
	
	@RequestMapping(value="/ticketDetail")
    public ModelAndView ticketDetail(@RequestParam("id") String ticketID
                               ) throws Exception {
		log.info("jinruhanshu ");
		ModelAndView mav=new ModelAndView();
		try {
			//获取工单信息及其扩展信息
			RestClientProxy lmrpItsmProxy=restClientFactory.getItsmGatewayClient();
			RestClientProxy traceProxy = restClientFactory.getAssignTraceClient();
			 if(Objects.nonNull(lmrpItsmProxy)&&Objects.nonNull(traceProxy)){
				 IItsmTicket iItsmTicket =lmrpItsmProxy.getService(IItsmTicket.class);
				 ITraceWorkflow traceWorkflow = traceProxy.getService(ITraceWorkflow.class);
				 	if(Objects.nonNull(iItsmTicket)&&Objects.nonNull(traceWorkflow)){
				 		RestResponse<TicketPO> ticket=iItsmTicket.getTicketExt("xihe", ticketID);
				 		RestResponse <List<AttachmentVO>> partsAttachmentList = iItsmTicket.listAttachment("xihe", ticketID, -1,"4,5");
				 	    // 获取流程节点
						//RestResponse<TicketProcessVO> tpVOResp = traceWorkflow.getTicketProcessVO("xihe", ticketID);
				 		if(Objects.nonNull(ticket)&&Objects.nonNull(partsAttachmentList)/*&&Objects.nonNull(tpVOResp)*/){
                             mav.addObject("chrsystem", (String)ticket.getResult().getOthers().getAttributeBean().get("chrsystem"));
                             mav.addObject("chrcategory",(String)ticket.getResult().getOthers().getAttributeBean().get("chrcategory"));
                             mav.addObject("chrtype",(String)ticket.getResult().getOthers().getAttributeBean().get("chrtype"));
                             mav.addObject("respondlevel",(String)ticket.getResult().getOthers().getAttributeBean().get("respondlevel"));
                             mav.addObject("description",(String)ticket.getResult().getOthers().getAttributeBean().get("description"));
                             mav.addObject("surveynote",(String)ticket.getResult().getOthers().getAttributeBean().get("surveynote"));
                             mav.addObject("customername",(String)ticket.getResult().getOthers().getAttributeBean().get("customername"));
                             mav.addObject("address",(String)ticket.getResult().getOthers().getAttributeBean().get("address"));
                             mav.addObject("customermobile",(String)ticket.getResult().getOthers().getAttributeBean().get("customermobile"));
                             mav.addObject("customerphone",(String)ticket.getResult().getOthers().getAttributeBean().get("customerphone"));
                             mav.addObject("actuallcustomer",(String)ticket.getResult().getOthers().getAttributeBean().get("actuallcustomer"));
                             mav.addObject("solution_summary",(String)ticket.getResult().getOthers().getAttributeBean().get("solution_summary"));
                             
                             mav.addObject("ticket", ticket.getResult());
                             mav.addObject("partsAttachmentList", partsAttachmentList.getResult());
                             //mav.addObject("ticketProcess", tpVOResp.getResult());
                             mav.setViewName("/ticket/detail");
				 		}else{
				 			log.error("ticket is null or empty");
				 		}
				 	}else{
				 		log.error("error no find IItsmTicket.class");
				 	}
			 }else{
				 log.error("error ItsmGateWayProxy interface exception");
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			 log.error(e.getMessage(),e);
		}
		mav.setViewName("/ticket/detail");  
        return mav;
    }
	
	@RequestMapping(value="/ticketProject")
    public String ticketProject() throws Exception {
        return "/ticket/projectInfo";
    }
	
	@RequestMapping(value="/ticketSearch")
	public ModelAndView search(@RequestParam("id") String ticketID,
            @RequestParam("name") String name,
            @RequestParam("status") String status
            ) throws Exception{
		 	final int MIN_VALUE = 2147483647;
			ModelAndView mav=new ModelAndView();
			  try{
			        RestClientProxy lmrpItsmProxy=restClientFactory.getItsmGatewayClient();
			        if(Objects.nonNull(lmrpItsmProxy)){
			        	IItsmTicket iItsmTicket =lmrpItsmProxy.getService(IItsmTicket.class);
			                if(Objects.nonNull(iItsmTicket)){
			                    RestResponse<PageData<TicketPO>> listTicket = iItsmTicket.listTicket("xihe", "%"+ticketID+"%", "%"+name+"%", status, 0, MIN_VALUE);
			                    if(Objects.nonNull(listTicket)){
			                        List<TicketPO> ticketList = listTicket.getResult().getPageData();
			                        //mav.addObject("startTime", startTime);
			                        mav.addObject("status",status);
			                        mav.addObject("ticketlist", ticketList);
			                        mav.addObject("id", ticketID);
			                        mav.addObject("name", name);
			                        mav.addObject("status", status);
			                        mav.setViewName("/ticket/show");
			                    }else{
			                        log.error("error query projectlist is null");
			                    }
			                }else{
			                    log.error("error no find IItsmProject.class");
			                }
			        }else{
			            log.error("error ItsmGateWayProxy interface exception");
			        }
			    }catch(Exception e){
			        log.error(e.getMessage(),e);
			    }
			mav.setViewName("/ticket/show");  
	        return mav;
	}
	
}
